package com.itboye.ihomebank.adapter;

import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.alibaba.mobileim.utility.ITaskReceiver;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.MyCollectionBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.XImageLoader;

/**
 * 收藏
 * 
 */
public class ShouCangAdapter extends MutiplyCommonAdapter<MyCollectionBean>
		implements Observer {
	List<MyCollectionBean> list;
	HousePresenter housePresenter;
	protected String uid;
	String tempHouseNo = null;

	public ShouCangAdapter(Context context, List<MyCollectionBean> datas,
			int... layoutId) {
		super(context, datas, layoutId);
		list = datas;
		housePresenter = new HousePresenter(this);
		uid = SPUtils.get(context, null, SPContants.USER_ID, "") + "";
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void getItemPosition(int position) {
		// TODO Auto-generated method stub

	}

	@Override
	public void convert(ViewHolder holder, final MyCollectionBean t, int type,
			int position) {
		// TODO Auto-generated method stub
		holder.setText(R.id.collection_status, t.getHouseNo());
		if (t.getMainImg() != null)
			XImageLoader.load(NetPublicConstant.IMAGE_URL
					+ t.getMainImg().split(",")[0],
					(ImageView) holder.getView(R.id.item_sou_img));
		holder.setText(R.id.item_sou_title, t.getTitle());
		holder.setText(R.id.item_sou_content,
				t.getHouseHall() + "室" + t.getHouseRoom() + "厅/" + t.getArea()
						+ "㎡/" + t.getOrientation() + "/" + t.getCommunity());
		holder.setText(R.id.item_sou_01, t.getTagNames());
		holder.setText(R.id.item_sou_money, t.getRent() + "元/月");

		holder.setOnClickListener(R.id.cancel_collection, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						housePresenter.collection(uid, "1", t.getHouseNo(), 0);
						tempHouseNo = t.getHouseNo();
					}
				});
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.collection_success) {
				ByAlert.alert(entity.getData());
				if (tempHouseNo != null) {
					Iterator<MyCollectionBean> iterator = list.iterator();
					while (iterator.hasNext()) {
						MyCollectionBean myCollectionBean = iterator.next();
						if (myCollectionBean.getHouseNo().equals(tempHouseNo)) {
							iterator.remove();
							notifyDataSetChanged();
							break;
						}

					}
				}
			} else if (entity.getEventType() == HousePresenter.collection_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}

}
